package com.gdut.huayan.controller;


import com.gdut.huayan.entity.dto.PageQuery;
import com.gdut.huayan.entity.dto.Result;
import com.gdut.huayan.entity.po.FileSystem;
import com.gdut.huayan.entity.vo.FileUpload;
import com.gdut.huayan.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/file")
public class FileController {

    @Autowired
    private FileService fileService;


    @PreAuthorize("hasAnyAuthority('NORMAL_USER','SUPER_ADMIN')")
    @PostMapping("/upload")
    public Result requestUpload(@RequestBody FileUpload fileUpload) {
        System.out.println(fileUpload.toString());
        return Result.success(fileService.requestUpload(fileUpload));
    }

    @PreAuthorize("permitAll()")
    @GetMapping("/download")
    public Result requestDownload(Long fileId, Long expireTime) {
        return Result.success(fileService.requestFile(fileId, expireTime, false));
    }

    @PreAuthorize("hasAnyAuthority('NORMAL_USER','SUPER_ADMIN')")
    @PostMapping("/update")
    public Result requestUpdate(@RequestBody FileUpload fileUpload) {
        return Result.success(fileService.requestUpdate(fileUpload, false));
    }

    @PreAuthorize("hasAnyAuthority('NORMAL_USER','SUPER_ADMIN')")
    @GetMapping("/updateConfirm")
    public Result requestUpdateConfirm(String requestId) {
        fileService.requestUpdateConfirm(requestId);
        return Result.success();
    }

    @PreAuthorize("hasAnyAuthority('NORMAL_USER','SUPER_ADMIN')")
    @DeleteMapping("/delete")
    public Result requestDelete(Long fileId) {
        fileService.requestDelete(fileId, false);
        return Result.success();
    }
}
